package com.zhuiyun.project.api.video.videoReview.service.impl;

import com.zhuiyun.project.api.video.publishVideo.entity.Video;
import com.zhuiyun.project.api.video.videoReview.entity.VideoReview;
import com.zhuiyun.project.api.video.videoReview.mapper.VideoReviewMapper;
import com.zhuiyun.project.api.video.videoReview.model.VideoReviewModel;
import com.zhuiyun.project.api.video.videoReview.service.VideoReviewService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName VideoReviewServiceImpl
 * @Description TODO
 * @Author Administrator
 * @Date 2023/12/26 9:59
 **/
@Service
@Transactional
public class VideoReviewServiceImpl implements VideoReviewService {

    @Autowired
    private VideoReviewMapper videoReviewMapper;
    /**
    * @Author qsh
    * @Description 未审核视频列表
    * @Date 2024/2/29 15:36
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param videoReviewModel
* @param pageRequest
    */
    @Override
    public CommonResult videoReviewList(VideoReviewModel videoReviewModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(videoReviewMapper.videoReviewList(videoReviewModel));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author qsh
    * @Description 审核通过视频列表
    * @Date 2024/2/29 15:36
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param videoReviewModel
* @param pageRequest
    */
    @Override
    public CommonResult videoReviewListT(VideoReviewModel videoReviewModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(videoReviewMapper.videoReviewListT(videoReviewModel));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author qsh
    * @Description 审核拒绝视频列表
    * @Date 2024/2/29 15:36
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param videoReviewModel
* @param pageRequest
    */
    @Override
    public CommonResult videoReviewListW(VideoReviewModel videoReviewModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(videoReviewMapper.videoReviewListW(videoReviewModel));
        return CommonResult.ok(pageResult);
    }

    /**
     * @Author qsh
     * @Description 审核视频
     * @Date 2024/2/23 14:32
     * @Return com.zhuiyun.project.common.response.CommonResult
     * @param videoIds
     */
    @Override
    public CommonResult ReviewVideo(Integer[] reviewIds,List<Integer> videoIds) throws CommonException {
        // 验证参数的规范性
        if (ValidatorUtil.ValidationObject(videoIds)) {
            // 创建 Example 对象
            int x = 0;
            for (Integer vId : videoIds) {
                Example example = new Example(VideoReview.class);
                example.createCriteria().andEqualTo("vId", vId); // 设置要删除的记录的主键ID条件
                List<VideoReview> publishVideos = videoReviewMapper.selectByExample(example);
                for (VideoReview reVideo : publishVideos) {
                    if (reVideo.getReviewStatus().equals("未审核") && reviewIds[x] == 0) {
                        reVideo.setReviewStatus("已通过");
                    } else if (reVideo.getReviewStatus().equals("未审核") && reviewIds[x] == 1) {
                        reVideo.setReviewStatus("已拒绝");
                    } else {
                        reVideo.setReviewStatus("未审核");
                    }
                    x = x+1;
                    videoReviewMapper.updateByExampleSelective(reVideo, example);
                }
            }
            return CommonResult.ok(EmErrorCode.UPDATE_OK);
        } else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
}
